<template>
  <!-- 常规布局开始 -->
  <div class="common-login">
    <!-- <span class="iconfont icon-shuyi_erweima"></span> -->
    <el-form-item label="用户名" prop="username">
      <el-input
        type="text"
        v-model="loginForm.username"
        autocomplete="off"
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        type="password"
        v-model="loginForm.password"
        autocomplete="off"
      ></el-input>
    </el-form-item>
    <el-form-item label="验证码" prop="captcha">
      <el-input
        maxlength="5"
        v-model.number="loginForm.captcha"
        type="text"
        ref="captcha"
        @keydown.native.enter="sunbmitForm('loginForm')"
      ></el-input>
    </el-form-item>
    <span class="captcha-svg" v-loading="loading">
      <i v-html="captcha" @click="getCaptcha"></i>
    </span>
    <el-form-item>
      <el-button type="primary" @click="submitForm('loginForm')">
        提交
      </el-button>
    </el-form-item>
    <!-- 手机登入图标按钮 -->
    <el-form-item>
      <div class="sms">
        <svg class="icon" aria-hidden="true"></svg>
        <a
          href="javascript:;"
          style="color: red; font: size 16px"
          class="a"
          @click="$emit('changeSmsType')"
        >
          切换短信登入
        </a>
      </div>
    </el-form-item>
  </div>
  <!-- 常规布局结束 -->
</template>

<script>
import * as api from "../../api/users";
// 表单验证的思路,当前组件的loginForm变化同步到父组件
export default {
  // props: ['loginForm'],
  data() {
    return {
      //控制短信验证的切换
      isSmsLogin: true,
      loading: false,
      captcha: "",
      loginForm: {
        username: "",
        password: "",
        captcha: "",
      },
    };
  },
  //   通过loginForm监听数据的变化
  watch: {
    loginForm: {
      handler(newVal) {
        // console.log(newVal);
        // 子传父触发父组件事件
        this.$emit("syncLoginForm", newVal);
      },
      deep: true,
    },
  },
  methods: {
    submitForm(formName) {
      // console.log(formName);
      //  console.log(this.$refs);
      this.$emit("submit");
    },
    //封装一个获取验证码的方法
    getCaptcha() {
      //获取验证码
      //打开加载动画
      this.loading = true;
      api.getCaptcha().then((res) => {
        // console.log(res);
        this.captcha = res.data.img;
        //关掉加载动画
        this.loading = false;
      });
    },
  },
  mounted() {},
  created() {
    this.getCaptcha();
  },
};
</script>

<style lang="less" scoped>
.captcha-svg {
  position: absolute;
  right: 300px;
  bottom: 200px;
}
.a {
  position: absolute;
  right: 200px;
  bottom: 190px;
  width: 100px;
}
// /deep/ .iconfont {
//   position: absolute;
//   font-size: 50px;
//   top: 0;
//   right: 0;
// }
.captcha-svg {
  position: absolute;
  top: 305px;
  right: 80px;
  cursor: pointer;
}
.el-loading-spinner {
  margin-top: -5px;
}
</style>
